<template>
  <el-dialog
    :visible="visible"
    :title="isUpdate ? '编辑' : '新增'"
    @close="visible = false"
  >
    <el-form class="form">
      <el-form-item label="父ID">
        <el-input v-model="formData.pid" />
      </el-form-item>
      <el-form-item label="IDS组合">
        <el-input v-model="formData.pids" />
      </el-form-item>
      <el-form-item label="IDS组合名称">
        <el-input v-model="formData.pidsName" />
      </el-form-item>
      <el-form-item label="行政区划名称">
        <el-input v-model="formData.name" />
      </el-form-item>
      <el-form-item label="英文名称">
        <el-input v-model="formData.englishName" />
      </el-form-item>
      <el-form-item label="行政区划代码">
        <el-input v-model="formData.code" />
      </el-form-item>
      <el-form-item label="层级">
        <el-input v-model="formData.level" />
      </el-form-item>
      <el-form-item label="层级名称">
        <el-input v-model="formData.levelName" />
      </el-form-item>
      <el-form-item label="国家ID">
        <el-input v-model="formData.countryId" />
      </el-form-item>
      <el-form-item label="备注信息">
        <el-input v-model="formData.remarks" />
      </el-form-item>
    </el-form>
    <el-button @click="visible = false">取 消</el-button>
    <el-button
      type="primary"
      @click="confirm"
    >确 定</el-button>
  </el-dialog>
</template>

<script>
import { getCompanyById, saveCompany, updateCompany } from '@/api/company'
import ImageUpload from '@/components/Upload/ImageUpload'
export default {
  components: { ImageUpload },
  data() {
    return {
      visible: false,
      isUpdate: false,
      formData: {
        id: '',
        pid: '',
        pids: '',
        name: '',
        englishName: '',
        code: '',
        level: '',
        levelName: '',
        countryId: '',
        remarks: ''
      },
      categoryList: []
    }
  },
  methods: {
    async init(id) {
      this.visible = true
      if (id != null) {
        this.isUpdate = true
        const data = await getCompanyById(id)
        this.formData = data.data
      } else {
        this.isUpdate = false
        this.formData = {
          id: '',
          pid: '',
          pids: '',
          name: '',
          englishName: '',
          code: '',
          level: '',
          levelName: '',
          countryId: '',
          remarks: ''
        }
      }
    },
    confirm() {
      if (this.isUpdate) {
        this.handleConform(updateCompany, this.formData)
      } else {
        this.handleConform(saveCompany, this.formData)
      }
    },
    handleConform(operation) {
      operation(this.formData).then(response => {
        if (response.statusCode == 20000) {
          this.refresh()
          this.visible = false
          this.$message({
            message: '操作成功',
            type: 'success'
          })
        } else {
          this.$message({
            message: '发生错误，操作失败',
            type: 'warning'
          })
        }
      })
    },
    refresh() {
      this.$emit('refresh')
    }
  }
}
</script>

<style>
.form {
  margin-bottom: 20px;
}
</style>
